import http from "@/utils/http";
import { getBaseUrl } from "@/utils/http";

export const getCommunications = () => {
  return http.get("device/communication/")
}

// ================================ 设备接口 ================================ //
export const createDevice = (data) => {
  return http.post("device/", data)
}

export const getDevices = () => {
  return http.get("device/list/")
}

export const getDeviceDetail = (no) => {
  return http.get(`device/`, { params: { no } })
}

export const modifyDevice = (no, data) => {
  return http.put(`device/`, data, { params: { no } })
}

export const deleteDevice = (no) => {
  return http.delete(`device/`, { params: { no } })
}

export const updateDeviceStatus = (no, disabled) => {
  return http.post("device/disabled/", { no, disabled })
}

// ================================ 点位接口 ================================ //
export const getGroups = (no) => {
  return http.get(`device/groups/`, { params: { no } })
}

export const deleteGroup = (no, group) => {
  return http.delete(`device/group/`, { params: { no, group } })
}

export const updateGroupName = (no, group, newName) => {
  return http.put(`device/group/`, {}, { params: { name: newName, no, group } })
}

export const getDevicePoints = (no) => {
  return http.get(`point/list/`, { params: { no } })
}

export const getPointsByGroup = (no, group) => {
  return http.get(`point/list/`, { params: { no, group } })
}

export const createPoint = (data) => {
  console.log(data)
  return http.post(
    `point/`, 
    {
      name: data.name, 
      func: data.func,
      addr: data.addr,
      desc: data.desc
    },
    { params: { no: data.no, group: data.group } }
  )
}

export const deletePoint = (id) => {
  return http.delete(`point/`, { params: { id } })
}

export const updatePoint = (id, data) => {
  return http.put(`point/`, data, { params: { id } })
}

export const checkPoint = (data) => {
  return http.post("point/batch/check/", data)
}

export const batchInputPoint = (data) => {
  return http.post("point/batch/input/", data)
}

export const downloadPointsExcel = () => {
  window.open(getBaseUrl() + "point/batch/output/excel/", "_blank")
}
